package com.lagou.homework;

import java.util.*;

public class ThreeTest {
    public static void main(String[] args) {
//          (1)定义一个学生类Student，包含属性：姓名(String name)、年龄(int age)
//          (2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
//          (3)利用四种方式遍历Map集合中的内容，格式：key::value
        Map m1 = new HashMap();
        m1.put(new Student("java",5),"四川");
        m1.put(new Student("python",1),"江西");
        System.out.println(m1);
        System.out.println("---------------------");
        /*1.keySet方法*/
        Set<Student> s1 = m1.keySet();
        for (Student s : s1){
            System.out.println(s+"::"+m1.get(s));

        }
        System.out.println("------------------");
        /*第二种 使用迭代器遍历集合*/
        Iterator s2 = s1.iterator();
        while(s2.hasNext()){
            Student key = (Student)s2.next();
            String value = (String)m1.get(key);
            System.out.println(key + "::" + value);
        }
        System.out.println("-----------");
        /*第三种 Entry*/
        Set<Map.Entry<Student,String>> s3 = m1.entrySet();
        for(Map.Entry<Student,String> s4:s3){
            Student key = s4.getKey();
            String value = s4.getValue();
            System.out.println(key+"::"+value);
        }
        System.out.println("------------");
        /*第四种 迭代器遍历key?  */
        Set<Student> s5 = m1.keySet();
        Iterator it1 = s5.iterator();
        while(it1.hasNext()){
            Student key = (Student) it1.next();
            String value = (String)m1.get(key);
            System.out.println(key+"::"+value);
        }
    }
}
